<?php

namespace App\Http\Controllers\Mp;

use App\Http\Controllers\Controller;
use App\Model\DeviceBus;
use App\Model\House;
use App\Model\HouseClass;
use App\Utils\Result;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class HouseClassController extends Controller
{
    /**创建&编辑
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function create(Request $request){
        $label = $request->input('label');
        $id = $request->input('id');
        if(!$label){
            return Result::error(1001,'请填写分组名称');
        }
        if(!$id){
            $class = new HouseClass();
        }else{
            $class = HouseClass::query()->where('id',$id)->first();
        }
        $class->label = $label;
        $class->business_id = $this->getBusinessId();
        $class->save();
        return Result::success($class);
    }

    /**删除
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request){
        $id = $request->input('id');
        if(!$id){
            return Result::error(1001,'缺少参数id');
        }
        $class = HouseClass::query()->find($id);
        if($class->business_id != $this->getBusinessId()){
            return Result::error(1001,'请删除自己名下分组');
        }
        DB::beginTransaction();
        try{
            House::query()->where('house_class_id',$id)->update(['house_class_id'=>0]);
            $class->delete();
            DB::commit();
        }catch(\Exception $e){
            DB::rollBack();
            return Result::error(1002,'操作繁忙 稍后重试');
        }
        return Result::success();
    }

    /**分组列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getList(Request $request){

        $businessId = $this->getBusinessId();
        $user = $this->getUser();
        if($user->type == 'admin'){
            $list = HouseClass::query()->whereIn('id',json_decode($user->class_id))->orderBy('sort','asc')->get();
        }else{
            $list = HouseClass::query()->where('business_id',$businessId)->orderBy('sort','asc')->get();
        }
        $type = $request->input('type','search');
        if($type == 'edit'){
            foreach($list as $k=> $v){
                $v->houses =  House::query()->where('house_class_id',$v->id)
                    ->orderBy('estate_name', 'asc')->orderBy('name', 'asc')
                    ->with( 'device','device.bus_device', 'lock')->get();
                $v->allDevice = 0;
                $v->onlineDevice = 0;
                foreach($v->houses as $vs){
                    foreach($vs->device as &$vd){
                       if($vd->online_status == 1) {
                           $v->onlineDevice++;
                       }
//                       $vd->bus_device = DeviceBus::query()
//                           ->where('device_id',$vd->id)
//                           ->first();
                       $v->allDevice++;
                    }
                }
            }
        }
        return Result::success($list);
    }

    /**获取单个分类下的房源
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getOne(Request $request){
        $id = $request->input('id');
        if(!$id){
            return Result::error(1002,'分类不存在');
        }
        $houses = House::query()->where('house_class_id',$id)
             ->orderBy('estate_name', 'asc')->orderBy('name', 'asc')
            ->with('contract', 'contract.contractTenant', 'contract.bills', 'device', 'lock')->get();
        $houses->allDevice = 0;
        $houses->onlineDevice = 0;
        foreach($houses as $k=>$v){
            foreach($v->device as $k1=>$v1){
                if($v1->online_status == 1){
                    $houses->onlineDevice+=1;
                }
                $houses->allDevice+=1;
            }
        }
        return Result::success($houses);
    }

    /**删除房源成功
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function clearClass(Request $request){
        $houseId = $request->input('house_id');
        if(!$houseId){
            return Result::error(1002,'房源不存在');
        }
        House::query()->where('id',$houseId)
            ->update(['house_class_id'=>0]);
        return Result::success();
    }

    /**获取未分组的房源
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getSelfHouses(Request $request){
        $businessId = $this->getBusinessId();
        $query = House::query()->where('business_id',$businessId);
            $query->where('house_class_id',0);
            $query->orderBy('estate_name', 'asc')->orderBy('name', 'asc');
            $houses = $query->with('contract', 'contract.contractTenant', 'device', 'lock')->get();
        return Result::success($houses);
    }
    /**
     * @param Request $request
     * @return void
     */
    public function batch(Request $request){
        $id = $request->input('id');
        $houses = $request->input('houses');
        if(!is_array($houses)){
            $houses = json_decode($houses,true);
        }
        $class = HouseClass::query()->find($id);
        if(!$class){
            return Result::error(1002,'分类不存在');
        }
        House::query()->whereIn('id',$houses)->update(['house_class_id'=>$id]);
        return Result::success();
    }

}
